<?php

namespace app\api\controller\xilumarket;

use think\Request;
use think\Controller;
use app\common\controller\Api;
use app\common\model\Autoreplay as ModelAutoreplay;

class Autoreplay extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $type = input('type', 'topic');
        $arp = [];
        switch ($type) {
            case 'topic':
                $arp = ModelAutoreplay::where([
                    'type_id' => input('topic_id'),
                    'type'  => $type
                ])->select();
                break;
            case 'product': 
                $arp = ModelAutoreplay::where([
                    'user_id' => $this->auth->id,
                    'type'  => $type
                ])->select();
                break;
        }
        $this->success('', $arp);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $type = input('type', 'topic');
        switch ($type) {
            case 'topic': 
                $arp = ModelAutoreplay::where([
                    'type_id' => input('topic_id'),
                    'type'  => input('type', 'topic')
                ])->count();
                if($arp >= 10){
                    $this->error('最多只能设置10条自动回复');
                }
                $replay = ModelAutoreplay::create([
                    'user_id' => $this->auth->id,
                    'type'  => $type,
                    'type_id'  => input('topic_id'),
                    'key'   => input('key'),
                    'replay' => input('replay'),
                    'remark' => input('remark'),
                ]);
                break;
            case 'product': 
                $arp = ModelAutoreplay::where([
                    'user_id' => $this->auth->id,
                    'type'  => input('type', 'topic')
                ])->count();
                if($arp >= 10){
                    $this->error('最多只能设置10条自动回复');
                }
                $replay = ModelAutoreplay::create([
                    'user_id' => $this->auth->id,
                    'type'  => $type,
                    'key'   => input('key'),
                    'replay' => input('replay'),
                    'remark' => input('remark'),
                ]);
                break;
        }
        $this->success('保存成功', $replay);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $arp = ModelAutoreplay::find($id);
        if(!$arp) {
            $this->error('该自动回复不存在');
        }
        $arp->save([
            'key'   => input('key'),
            'replay' => input('replay'),
            'remark' => input('remark'),
        ]);
        $this->success('保存成功', $arp);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $arp = ModelAutoreplay::destroy($id);
        $arp && $this->success('删除成功');
        $this->error('删除失败');
    }
}
